package mest.service.LGF.WarehouseTakeMaterial.Impl;

import mest.VO.LGF.WarehouseTakeMaterial.warehouse_productjob_takematerial_recordVO;
import mest.dao.LGF.WarehouseTakeMaterial.IWarehouseTakeMaterialDao;
import mest.dao.LGF.WarehouseWorkFinish.IWarehouseWorkFinishDao;
import mest.service.LGF.WarehouseTakeMaterial.IWarehouseTakeMaterialService;
import mest.util.mestUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-12-22 19:06
 */
@Service
public class WarehouseTakeMaterialServiceImpl implements IWarehouseTakeMaterialService {

    @Resource
    private IWarehouseTakeMaterialDao iwtd;
    @Resource
    HttpServletRequest request;
    //    String theirDb = mestUtil.getCompanyDB(request);


    @Override
    public Map baseSelectNofinish(int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = iwtd.countNOFinish(theirDb);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",iwtd.selectAllNOFinish(theirDb,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public Map baseSelectFinish(int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = iwtd.countFinish(theirDb);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",iwtd.selectAllFinish(theirDb,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public Map selectTakeMaterialDetail(int wtId, int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = iwtd.selectTakeMaterialDetailCount(theirDb,wtId);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",iwtd.selectTakeMaterialDetail(theirDb,wtId,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public Map selectTakeMaterialOutPastRecord(int wptId, int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = iwtd.selectTakeMaterialOutPastRecordCount(theirDb,wptId);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",iwtd.selectTakeMaterialOutPastRecord(theirDb,wptId,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public int addRecord(warehouse_productjob_takematerial_recordVO wptr) throws Exception{
        String theirDb = mestUtil.getCompanyDB(request);
        wptr.setTheirDb(theirDb);
        int i = 0;
        	//先判断有没有库存,然后修改库存
        
        Map<String, Object> wsMap = iwtd.existWarehouseSave(wptr);
        if (wsMap == null || Integer.valueOf(""+wsMap.get("number")) < wptr.getThisNumber() ) {
			//为空就跳出
			throw new Exception("库存不足");
		} else {
			wptr.setWsmId(Integer.valueOf(wsMap.get("id")+""));
			i = iwtd.updateWarehouseSave(wptr);
			if (i < 1) {throw new Exception("更新库存失败");}
			iwtd.insertWSRecord(wptr);
			if (i < 1) {throw new Exception("更新库存日志失败");}
		}
        
        i = iwtd.addRecord(wptr);
        if (i < 1) {
			throw new Exception("更新领料日志失败");
		}
        i = iwtd.updatehasBeenNumber(wptr);
        if (i < 1) {
			throw new Exception("更新领料出库总数失败");
		}
        return i;
    }

    @Override
    public int updateByWTId(int wtId, int finishStatus) {
        String theirDb = mestUtil.getCompanyDB(request);
        int i =-2;
        try {
            i = iwtd.updateByWTId(theirDb,wtId,finishStatus);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }
}
